<?

  class ManageController extends CFD_Controller_Action

  {

    public function init()

	{

	  parent::init();

	  $this->isAdmin(true);

	}



	public function statsAction()

	{

	  $this->view->title = "System stats";

		

	  $voucherTable = new Application_Model_Vouchers();

	  $res = $voucherTable->getAdapter()->query("SELECT COUNT(*) as count FROM vouchers");

	  $count = $res->fetchAll();

	  $this->view->allVouchers = $count[0]['count'];

	  

	  $voucherTable = new Application_Model_Vouchers();

	  $res = $voucherTable->getAdapter()->query("SELECT COUNT(*) as count FROM vouchers where purchase_users_id != 0");

	  $count = $res->fetchAll();

	  $this->view->purchased = $count[0]['count'];



	  $voucherTable = new Application_Model_Vouchers();

	  $res = $voucherTable->getAdapter()->query("SELECT COUNT(*) as count FROM vouchers where exp_date < '".date("Y-m-d")."'");

	  $count = $res->fetchAll();

	  $this->view->purchased = $count[0]['count'];

	  

	  $voucherTable = new Application_Model_Vouchers();

	  $res = $voucherTable->getAdapter()->query("SELECT COUNT(*) as count FROM vouchers where exp_date >= '".date("Y-m-d")."'");

	  $count = $res->fetchAll();

	  $this->view->active = $count[0]['count'];

        $valTable = new Application_Model_Vouchers();

		$select = $valTable ->select();

		$this->view->valuationsListFiltered = $this->view->valuationsList = $valTable->fetchAll($select);






	}



	public function addVoucherCodeAction()

	{

	  $this->view->title = "Add voucehrs to the datbase";

	  $this->view->hideResult = true;



	  if ($_POST['vouchers'])

	  {



		$cols = array();

		foreach ($_POST as $k => $v)

		  if (strpos($k, "col_") === 0)

		    $cols[ $v ] = (int)substr($k, 4)-1;



		$voucherTable = new Application_Model_Vouchers();

	    $vList = explode("\n", $_POST['vouchers']);



		$vouchers = array();

		foreach ($vList as $line)

		{

		  $data = explode($_POST['separator'], $line);



		  $v = array('voucher' => trim($data[ $cols['voucher'] ]), 'exp_date' => trim($data[$cols['exp_date']]), 'type' => trim($data[$cols['type']]), 'details' => trim($data[ $cols['detail'] ]), 'exists' => false);



		  if ($v['voucher'])

		  {

		    $row = $voucherTable->fetchRow( $voucherTable->select()->where('voucher = ?', $v['voucher']) );



			if ($row)

			  $v['exists'] = true;



		    $vouchers[] =  $v;

		  }

	 

		}

		$this->view->vouchers = $vouchers;

		$this->view->hideForm = true;

		$this->view->hideResult = false;

	  }



	  if ($_POST['serVouchers'])

	  {

		$this->view->hideForm = true;

		$this->view->hideResult = true;

	    $vouchers = unserialize(base64_decode($_POST['serVouchers']));



		$voucherTable = new Application_Model_Vouchers();

		foreach ($vouchers as $voucher)

		{

		  $voucherRow = $voucherTable->createRow($voucher);

		  try

		  {

		    $voucherRow->save();

		  }

		  catch (Exception $e)

		  {

	

		  }

		}



	  	Alert_View::addMessage(Alert_View::NOTICE, "", "New vouchers were successfully loaded to the database");

	  }

	}


	public function deleteAction() {
		$id = $_GET['id'];
		if ($id == '')
			return;
		$this->isAdmin(true);

		$voucherTable = new Application_Model_Vouchers();
		$res = $voucherTable->getAdapter()->query("delete FROM vouchers where id = " . addslashes($id));
		$this->getResponse()->setRedirect('/manage/stats');

	}

	public function usersAction()

	{

	  $this->view->title = "Manage users";



	  $usersTable = new Application_Model_Users();



	  if ($_GET['ch'])

	  {

	    $usersTable = new Application_Model_Users();

		if ($usersTableRow = $usersTable->find((int)$_GET['ch']))

		{

  		  $usersTableRow[0]->user_enabled = !($usersTableRow[0]->user_enabled);

  		  $usersTableRow[0]->save();

		}

	  }



	  $adapter = new Zend_Paginator_Adapter_DbSelect($usersTable->select()->order('id DESC'));



	  $this->view->page = $this->_request->getParam('page');



	  if (!$this->view->page)

	    $this->view->page = 1;



	  $res = $usersTable->getAdapter()->query("SELECT COUNT(*) as count FROM users");

	  $count = $res->fetchAll();

	  $count = $count[0]['count'];



	  $adapter->setRowCount( (int)$count );



	  $paginator = $this->view->paginator = new Zend_Paginator($adapter);

	  $paginator->setItemCountPerPage(7);

	  $paginator->setCurrentPageNumber($this->_request->getParam('page'));

	}

  }

